package controller

import (
	myConfig "common/config"
	"common/helper"
	myredis "common/redis"
	myUserHelp "common/userHelp"
	"encoding/json"
	"fmt"
	"github.com/valyala/fasthttp"
	"member/model"
	"time"
)

type TurnTableController struct{}

type ClientCfg struct {
	TurnTableType         int     `json:"turnTableType" db:"turnTableType" cbor:"turnTableType"`
	NeedLuckyPoint        float64 `json:"NeedLuckyPoint" db:"NeedLuckyPoint" cbor:"NeedLuckyPoint"`
	NeedSilverLuckyPoint  float64 `json:"NeedSilverLuckyPoint" db:"NeedSilverLuckyPoint" cbor:"NeedSilverLuckyPoint"`
	NeedGoldLuckyPoint    float64 `json:"NeedGoldLuckyPoint" db:"NeedGoldLuckyPoint" cbor:"NeedGoldLuckyPoint"`
	NeedDiamondLuckyPoint float64 `json:"NeedDiamondLuckyPoint" db:"NeedDiamondLuckyPoint" cbor:"NeedDiamondLuckyPoint"`
	TurnTable             []myConfig.CfgTClientItem
}

func (that *TurnTableController) TurnTableCfg(ctx *fasthttp.RequestCtx) {

	param := TurnTableTypeReq{}

	dataBody := ctx.PostBody()
	err := json.Unmarshal(dataBody, &param)
	if err != nil || param.TurnTableType < 1 || param.TurnTableType > 3 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	var clientRes ClientCfg
	clientRes.TurnTable = myConfig.GetTurnTableCfg(param.TurnTableType)
	clientRes.TurnTableType = param.TurnTableType

	NeedLuckPoint := 0.0
	if param.TurnTableType == myConfig.TurnTable_Silver {
		NeedLuckPoint = myConfig.GetGameCfgFloat64(myConfig.CfgSilverTurnTableComs)
	} else if param.TurnTableType == myConfig.TurnTable_Gold {
		NeedLuckPoint = myConfig.GetGameCfgFloat64(myConfig.CfgGoldTurnTableComs)
	} else if param.TurnTableType == myConfig.TurnTable_Diamond {
		NeedLuckPoint = myConfig.GetGameCfgFloat64(myConfig.CfgDiamondTurnTableComs)
	}

	clientRes.NeedLuckyPoint = NeedLuckPoint
	clientRes.NeedSilverLuckyPoint = myConfig.GetGameCfgFloat64(myConfig.CfgSilverTurnTableComs)
	clientRes.NeedGoldLuckyPoint = myConfig.GetGameCfgFloat64(myConfig.CfgGoldTurnTableComs)
	clientRes.NeedDiamondLuckyPoint = myConfig.GetGameCfgFloat64(myConfig.CfgDiamondTurnTableComs)
	helper.Print(ctx, true, clientRes)
}

type TurnTableTypeReq struct {
	TurnTableType int `json:"TurnTableType"`
}

func (that *TurnTableController) Click(ctx *fasthttp.RequestCtx) {

	uid := model.GetUidFromToken(ctx)

	param := TurnTableTypeReq{}

	dataBody := ctx.PostBody()
	err := json.Unmarshal(dataBody, &param)
	if err != nil || param.TurnTableType < 1 || param.TurnTableType > 3 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	if myConfig.IsActiveOpen(myConfig.ActiveType_TurnTable) == 0 {
		helper.Print(ctx, false, helper.AppActiveClose)
		return
	}

	NeedLuckPoint := 0.0
	if param.TurnTableType == myConfig.TurnTable_Silver {
		NeedLuckPoint = myConfig.GetGameCfgFloat64(myConfig.CfgSilverTurnTableComs)
	} else if param.TurnTableType == myConfig.TurnTable_Gold {
		NeedLuckPoint = myConfig.GetGameCfgFloat64(myConfig.CfgGoldTurnTableComs)
	} else if param.TurnTableType == myConfig.TurnTable_Diamond {
		NeedLuckPoint = myConfig.GetGameCfgFloat64(myConfig.CfgDiamondTurnTableComs)
	}

	if NeedLuckPoint > myUserHelp.GetEnableLuckyPoint(uid) {
		helper.Print(ctx, false, helper.LackOfTurnTableLuckyPoint)
		return
	}

	var lottery myConfig.CfgTClientItem
	lottery.Id, lottery.LuckyPoint = myConfig.GetTurnTableAward(param.TurnTableType)

	myUserHelp.AddEnableLuckyPoint(uid, -NeedLuckPoint)
	myUserHelp.AddUsedLuckyPoint(uid, NeedLuckPoint)

	myUserHelp.AddBalanceTransactionRecord(uid, lottery.LuckyPoint, helper.TransactionTurnTable, "0", helper.GenId(), 0.00, "", "", 0)
	myUserHelp.AddUserWageItem(uid, lottery.LuckyPoint, helper.TransactionTurnTable)
	myUserHelp.AddUserBalanceByFloat64(uid, lottery.LuckyPoint)

	AccountName := myUserHelp.GetMemberName(uid)
	//抽奖记录
	sqlInsert := fmt.Sprintf("insert into tbl_rec_turntable(uid,account_name,type,award,used_lucky_point,created_at) "+
		"values(%s,'%s',%d,%0.2f,%0.2f,%d)",
		uid, AccountName, param.TurnTableType, lottery.LuckyPoint, NeedLuckPoint, time.Now().Unix())
	myredis.AddLogToRedis(sqlInsert)

	helper.Print(ctx, true, lottery)
}

// 查询自己的抽奖记录 最近10条

type TurnTableRecordItem struct {
	Uid            int     `json:"uid" db:"uid" cbor:"uid"`
	AccountName    string  `json:"account_name" db:"account_name" cbor:"account_name"`
	Type           int     `json:"type" db:"type" cbor:"type"`
	Award          float64 `json:"award" db:"award" cbor:"award"`
	UsedLuckyPoint float64 `json:"used_lucky_point" db:"used_lucky_point" cbor:"used_lucky_point"`
	Createdat      int     `json:"created_at" db:"created_at" cbor:"created_at"`
}

type memberTurnTableInfoRes struct {
	EnableLuckyPoint  float64 `json:"enable_lucky_point" db:"enable_lucky_point" cbor:"enable_lucky_point"`
	ExpiredLuckyPoint float64 `json:"expired_lucky_point" db:"expired_lucky_point" cbor:"expired_lucky_point"`
	UsedLuckyPoint    float64 `json:"used_lucky_point" db:"used_lucky_point" cbor:"used_lucky_point"`
	ValidRunning      float64 `json:"valid_running" db:"valid_running" cbor:"valid_running"`
}

func (that *TurnTableController) UserTurnTableInfo(ctx *fasthttp.RequestCtx) {
	uid := model.GetUidFromToken(ctx)
	var res memberTurnTableInfoRes

	res.EnableLuckyPoint = myUserHelp.GetEnableLuckyPoint(uid)
	res.ExpiredLuckyPoint = myUserHelp.GetExpiredLuckyPoint(uid)
	res.UsedLuckyPoint = myUserHelp.GetUsedLuckyPoint(uid)
	res.ValidRunning = myUserHelp.GetTotalWaged(uid)
	helper.Print(ctx, true, res)
}
func (that *TurnTableController) TurnTableRecord(ctx *fasthttp.RequestCtx) {
	uid := model.GetUidFromToken(ctx)

	SqlSelect := fmt.Sprintf("Select uid,account_name,type,award,used_lucky_point,created_at from tbl_rec_turntable "+
		"	where uid=%s "+
		"	order by created_at desc", uid)

	var recordLst []TurnTableRecordItem
	err := myredis.GetMasterDBInstance().Select(&recordLst, SqlSelect)
	if err != nil {
		helper.SqlErrLog("TurnTableRecord:%s----%s", SqlSelect, err.Error())
	}

	helper.Print(ctx, true, recordLst)
}

func (that *TurnTableController) TurnTableOtherRecord(ctx *fasthttp.RequestCtx) {

	SqlSelect := fmt.Sprintf("Select uid,account_name,type,award,used_lucky_point,created_at from tbl_rec_turntable" +
		"	where  award > 0 " +
		"	order by created_at desc limit 30")

	var recordLst []TurnTableRecordItem
	err := myredis.GetMasterDBInstance().Select(&recordLst, SqlSelect)
	if err != nil {
		helper.SqlErrLog("TurnTableRecord:%s----%s", SqlSelect, err.Error())
	}

	helper.Print(ctx, true, recordLst)
}
